from django.db import models
from django.template.defaultfilters import slugify

class Category(models.Model):
    """Site level category management class"""
    name = models.CharField(max_length=20)
    slug = models.SlugField(max_length=100, editable=False, unique=True)
    description = models.TextField(null=True, blank=True)
    parent = models.ForeignKey('self', null=True, blank=True)
    
    def __unicode__(self):
        return self.path
    
    def save(self):
        if not self.id:
            self.slug = slugify(self.name)
        
        super(Category, self).save()
    
    def get_path(self, sep=' / '):
        if self.parent:
            return u'%s%s%s' % (self.parent.get_path(sep), sep, self.name)
        else:
            return self.name
    path = property(get_path)
    
class Tag(models.Model):
    """
    Site level tag management. This class only handles simple tags,
    for machine tags see the MachineTag class below.
    """
    name = models.CharField(max_length=30, unique=True)
    
    def __unicode__(self):
        return self.name

class MachineTag(models.Model):
    """
    Machine tags model class. Allows using machine tags for
    every object in the site.
    """
    namespace = models.CharField(max_length=30)
    predicate = models.CharField(max_length=30)
    value = models.CharField(max_length=30, null=True, blank=True)
    
    def __unicode__(self):
        return '%s:%s=%s' % (self.namespace, self.predicate, self.value)
    
    class Meta:
        unique_together = (('namespace', 'predicate', 'value'),)

